from random import randint, choice
import copy
import time
import os
from jplur.NameGenerator import *
import jplur

class Generator:
	def __init__(self):
		self.list = {}
		self.namer_easy = NameGenerator(randint(0,3000), 3)
		self.namer_normal = NameGenerator(randint(0,3000), 3)
		self.namer_hard = NameGenerator(randint(0,3000), 3)
		
		self.namer_easy.inputFile("./edit/names/monster_easy.txt")
		self.namer_normal.inputFile(os.path.join("./edit/names/monster_normal.txt"))
		self.namer_hard.inputFile(os.path.join("./edit/names/monster_hard.txt"))
		
		monsters = []
		for i in range(40):
			level = []
			for j in range(10):
				level.append( self.make(i) )
			monsters.append(level)
		self.monsters = monsters
		
	def betterName(self, namer):
		name = []
		r = randint(0,14) 
		if r < 7:
			name = [random.choice(namer.words)]
		elif r < 8:
			name = [random.choice(namer.words), random.choice(namer.words)]
		elif r < 12:
			name = [namer.makeName()+namer.makeName()]
		elif r < 13:
			name = [namer.makeName()+namer.makeName(), random.choice(namer.words) ]
		else:
			name = [namer.makeName(), namer.makeName()+namer.makeName()]
			
		return name
		
	def make(self, level):
		colors = ["9b4040","8b9b40","409b6d", "68b8d6", "5328a3", "555555", 
					"bbbbbb", "ffffff", "17791c", "fc1004", "ce8fd7", "0d82ae", "2fe73c", "9b6a00", "009b49"]
		#generate a random monster template appropriate for the level
		if randint(0,100) < 20:
			level += randint(0,2)-1
		
		if level <= 0:
			level = 1
		
		if level < 5:
			species = self.betterName( self.namer_easy )
		elif level < 10:
			species = self.betterName( self.namer_normal )
		else:
			species = self.betterName( self.namer_hard )
		symbol = species[-1:][0][:1].lower()
		if level > 10:
			symbol = symbol.upper()
			
		for entry in species:
			entry = entry[:1].lower()+entry[1:]
		species = " ".join(species)
		name = "the "+species
		
		
			
		
		size = 10 * randint(1,level)
		speed = .2 + 1/(1.0 * (randint(1,level)))
		
		str = 10
		intel = 10
		con = 10
		dex = 10
		
		flags = []
		hit_die = [ int(randint(1,level)*.8),randint(1,level)] 
		attack_die = [ 1+int(randint(1,level)*.3),int(1 + randint(1,level)*.8)] 
		color = choice(colors)
		packet = [species, name, level, size, speed, str, intel, con, dex, flags, hit_die, symbol, color, attack_die]
		return packet
		
	def get(self, level):
		#get a type of monster for the apropriate level
		level = level+(randint(0,2)-1)-2
		if level < 0:
			level = 0
		return choice(self.monsters[level])
		
	def save(self):
		pass
		#save the gen'd monsters
		
	def load(self):
		pass
		#load the gen'd monsters
